package com.examlpe.cels.service.impl;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import com.examlpe.cels.dao.IBaseTermDao;
import com.examlpe.cels.dao.impl.BaseTermDaoImpl;
import com.examlpe.cels.entity.Vocabulary;
import com.examlpe.cels.entity.Word;
import com.examlpe.cels.exception.FileNotFoundException;
import com.examlpe.cels.service.IBrowserService;
import com.examlpe.cels.util.PageBean;

public class BrowserServiceImpl implements IBrowserService {

	IBaseTermDao dao = new BaseTermDaoImpl();
	
	//@Override
	public Map<Character, Set<Word>> getFirstMap() {
		Map<Character, Set<Word>> datas = new TreeMap<Character, Set<Word>>();
		try {
			Set<Word> wSet = dao.getAllWords();
			Set<Word> set = null;
			for(Word w:wSet) {
				if(datas.containsKey(w.getFirst())) {
				  set = datas.get(w.getFirst());
				  set.add(w);
				  datas.put(w.getFirst(), set);
				}else {
				  set = new TreeSet<Word>();
				  set.add(w);
				  datas.put(w.getFirst(), set);
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}
		return datas;
	}

	//@Override
	public PageBean getPageVocabularies() {
		Set<Vocabulary> datas  = null;
		try {
			datas = dao.getAllVocabularies();
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}
		int pageSize = IBrowserService.PAGE_SIZE;
		PageBean pb = new PageBean(datas, pageSize); 
		return pb;
	}

}
